{% extends "emptybase.html" %}

{% block jsblock %}
<script type="text/javascript">
<!--
//this block check the exist username and pwd valid

$userexist = false;
$userpwd = false;

function checkuser(){
    if($userexist == false){
        alert("user not exist");
        return false;
    }
    else if($userpwd == false){
        alert("password error");
        return false;
    }
    return true;
}

function checkUsernamePwd(){
    $.getJSON("/basicuser/checkusernamepwd?username="+$("#username").attr("value")+"&pwd="+$("#pwd").attr("value"),checkUserResult);
}

function checkUserResult(data){
    if(data.status == "good"){
        $userpwd = true;
    }else{
        //$("#errorinfo").html(data.thedesp);
        $("#id_pwd_hidden").html("password error");
        return false;
    }
}

function checkUserName(){
     $.getJSON("/basicuser/checkusername?name="+$("#username").attr("value"),checkUserNameResult);
}

function checkUserNameResult(data){
    //$("#id_user_hidden").attr("hidden","false");
    if(data.status == "good"){
        //$("#id_user_hidden").innerHTML = "username ok";
        $("#id_user_hidden").html("user not exist");
        $userexist = true;
    }
    else if(data.status == "bad"){
        $("#id_user_hidden").html("");
        $userexist = false;
    }
    else{
        alert("status not right:"+data.status);
        $userexist = false;
    }
}


function initevent(){
    $("#username").keyup(checkUserName);
    $("#pwd").keyup(checkUsernamePwd);
    checkUserName();
}

$(initevent);

-->
</script>
{% endblock %}

{% block bodyblock %}
<form action="/basicuser/connectexistuser" method="post">
    {% csrf_token %}
    
    {% if sinaid %}
    <input type="hidden" name="sinaid" value="{{ sinaid }}" />
    {% endif %}

    {% if doubanid %}
    <input type="hidden" name="doubanid" value="{{ doubanid }}" />
    {% endif %}
    <div>
        <label>username:</label>
        <input type="text" id="username" name="username" value="{{ username }}">
        <label id="id_user_hidden"></label>
    </div>
    <div>
        <label>password:</label>
        <input type="password" id="pwd" name="pwd">
        <label id="id_pwd_hidden"></label>
    </div>
    <div><input type="submit" value="Connect" onclick="return checkuser()"></div>
    <div><a href="/basicuser/connectnewuser?username={{username}}&sinaid={{sinaid}}&doubanid={{doubanid}}">Connect a new User</a>
    </form>

    <div id="errorinfo">
    </div>
{% endblock %}
